package cn.tinlan.shopcoin.app.mine.order;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.ToastUtils;
import cn.tinlan.shopcoin.base.BaseActivity;

/**
 * Created by Mortimer on 2018/11/27. 09:34
 * 佛祖🙏，永无bug。
 */
public class OrderActivity extends BaseActivity {

    @BindView(R.id.order_titlebar)
    TitleBar order_titlebar;

    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
//    @BindView(R.id.viewpager)
//    ViewPager viewPager;

    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private FragmentContainerHelper mFragmentContainerHelper = new FragmentContainerHelper();
    //List name = Arrays.asList("xxx","yyy","zzz");这种写法 list的size是固定的;
    // List name =new ArrayList<>(Arrays.asList("xxx","yyy","zzz"));这种写法,list的不是fixed的
    private List<String> titles = Arrays.asList("全部","待付款","待收货","待评价");//
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_order;
    }


    @Override
    public void initView() {
        super.initView();

        drawableDefaultBar();

        initTitleBar();
        initFragments();
        initMagicIndicator();

        int index = getIntent().getIntExtra("position",1);
        mFragmentContainerHelper.handlePageSelected(index, false);
        switchPages(index);

    }

    private void initTitleBar() {
        order_titlebar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {
                ToastUtils.show("功能暂无");
            }
        });

    }

    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setScrollPivotX(0.8f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.size();
            }
            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {


                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(titles.get(index));
                simplePagerTitleView.setNormalColor(Color.parseColor("#333333"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#FD7F35"));

                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        viewPager.setCurrentItem(index);
                        mFragmentContainerHelper.handlePageSelected(index);
                        switchPages(index);
                    }
                });
                return simplePagerTitleView;
//                ClipPagerTitleView clipPagerTitleView = new ClipPagerTitleView(context);
//
//                clipPagerTitleView.setText(titles.get(index));
//                clipPagerTitleView.setTextColor(Color.parseColor("#FD7F35"));
//
//                clipPagerTitleView.setClipColor(Color.WHITE);
//
//                clipPagerTitleView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
////                        viewPager.setCurrentItem(index);
//                        mFragmentContainerHelper.handlePageSelected(index);
//                        switchPages(index);
//                    }
//                });
//
//                return clipPagerTitleView;

            }
//            @Override
//            public IPagerIndicator getIndicator(Context context) {
//                return null;
//            }
            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setYOffset(UIUtil.dip2px(context, 3));
//                float navigatorHeight = context.getResources().getDimension(R.dimen.indiviheight);
//                float borderWidth = UIUtil.dip2px(context, 1);
//                float lineHeight = navigatorHeight - 2 * borderWidth;
//                indicator.setLineHeight(R.dimen.indiviheight);
//                indicator.setRoundRadius(lineHeight / 2);
//                indicator.setYOffset(borderWidth);
                indicator.setColors(Color.parseColor("#FD7F35"));
                return indicator;
            }

//            @Override
//            public float getTitleWeight(Context context, int index) {
//                return 1.2f;
//                return super.getTitleWeight(context, index);
//            }
        });
        magicIndicator.setNavigator(commonNavigator);

//        ViewPagerHelper.bind(magicIndicator, viewPager);
        mFragmentContainerHelper.attachMagicIndicator(magicIndicator);
    }


    private void initFragments() {


        for (int i = 0; i < titles.size(); i++) {
            OrderFragment orderFragment = new OrderFragment();
            orderFragment.orderType = i ;
            mFragments.add(orderFragment);
//            OrderFragment nopay = new OrderFragment();
//            OrderFragment noget = new OrderFragment();
//            OrderFragment nocomment = new OrderFragment();
////            Bundle bundle = new Bundle();
////            bundle.putString(TestFragment.EXTRA_TEXT, CHANNELS[i]);
////            testFragment.setArguments(bundle);
//            mFragments.add(all);
//            mFragments.add(nopay);
//            mFragments.add(noget);
//            mFragments.add(nocomment);
        }

    }

    private void switchPages(int index) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment;
        for (int i = 0, j = mFragments.size(); i < j; i++) {
            if (i == index) {
                continue;
            }
            fragment = mFragments.get(i);
            if (fragment.isAdded()) {
                fragmentTransaction.hide(fragment);
            }
        }
        fragment = mFragments.get(index);
        if (fragment.isAdded()) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.add(R.id.fragment_container, fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();
    }
}
